<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<view class="content detail">
			<!--需求信息-->
			<view class="detail-item">
				<uni-section title="需求信息" type="line"></uni-section>
				<view class="detail-content">
					<view class="detail-text">需求编号：<text>{{info.number}}</text></view>
					<view class="detail-text">配送仓：<text>{{info.warehouseName}}</text></view>
					<view class="detail-text">区域代码：<text>{{info.regionCode}}</text></view>
					<view class="detail-text">分拣仓：<text>{{info.fenjianWarehouseName}}</text></view>
					<view class="detail-text">制单时间：<text>{{info.createTime}}</text></view>
					<view class="detail-text">审核时间：<text>{{info.confirmTime}}</text></view>
					<view class="detail-text">执行时间：<text>{{info.executeTime}}</text></view>
					<view class="detail-text">销需状态：<uni-tag size="small" :text="arrText(allStatusList, info.status)" /></view>
					<view class="detail-text">销需说明：<text>{{info.remark}}</text></view>
				</view>
			</view>
			
			<!--需求商品-->
			<view class="detail-item">
				<uni-section title="需求商品" type="line"></uni-section>
				<view class="detail-content table">
					<ty-table
						:data="demandProductTableList"
						:columns="demandProductColumns"
						:sortable="true"
						:cellClassName="() => 'table-cell'"
						:headerCellClassName="() => 'table-cell'"
						:rowClassName="() => 'table-row'"
						stripe
						border>
						<template v-slot:before="{ row }">
							<view class="table-td-before table-cell table-td-row">
								<view class="table-td-row">
									商品名称：
									<text class="table-td-text">{{row.productName}}</text></view>
								<view class="table-td-row">
									商品编码：
									<text class="table-td-text">{{row.productCode}}</text></view>
							</view>
						</template>
						<template v-slot:id="{ row }">
							<view class="table-td-cell">
								<view class="table-td-row">
									序号：
									<text class="table-td-text">{{row.id}}</text></view>
								<view class="table-td-row">
									规格：
									<text class="table-td-text">{{row.productUnit}}</text></view>
							</view>
						</template>
						<template v-slot:count="{ row }">
							<view class="table-td-cell">
								<view class="table-td-row">
									需求数量：
									<text class="table-td-text">{{row.demandNumber}}</text></view>
								<view class="table-td-row">
									审核数量：
									<text class="table-td-text">{{row.auditNumber}}</text></view>
							</view>
						</template>
						<template v-slot:status="{ row }">
							<view class="table-td-cell">
								<view class="table-td-row">
									状态：
									<uni-tag size="small" :type="row.status === 1 ? 'success' : ''" :text="arrText(salesmanDemandProductStatusList, row.status)" /></view>
								<view class="table-td-row">
									备注：
									<text class="table-td-text">{{row.remark}}</text></view>
							</view>
						</template>
					</ty-table>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref } from 'vue';
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { salesmanDemandDetail, salesmanDemandProduct } from "@/api/erp_api/sale"
import { storageProduct } from "@/api/erp_api/storageProduct"
import { salesmanDemandStatus } from "@/api/system/dict/data"
import { copyText, arrText } from '@/utils/ty.js'
import { salesmanDemandProductStatusList } from '@/utils/config.js'
import useUserStore from '@/store/modules/user'

	const userStore = useUserStore()
	onLoad(async (option) => {
		if (option.id) {
			id.value = option.id
			await getSalesmanDemandStatus()
			getDetail()
			searchFirst()
		}
	})
	
	// 采购需求状态
	const allStatusList = ref([])
	const getSalesmanDemandStatus = () => {
		return salesmanDemandStatus().then(res => {
			allStatusList.value = res.data.map(item => {
				return {
					...item,
					value: item.dictValue,
					text: item.dictLabel
				}
			}) || []
		})
	}
	
	// 数据查询
	const id = ref('')
	const info = ref({})
	const isLoading = ref(false)
	
	const getDetail = () => {
		const params = {
			id: id.value
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		salesmanDemandDetail(params).then(res => {
			info.value = res.data || {}
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 需求商品
	const demandProductColumns = ref([
		{ title: '编号规格', key: 'id' },
		{ title: '数量', key: 'count', width: '200rpx' },
		{ title: '状态备注', key: 'status' }
	])
	
	const pageNum = ref(1)
	const pageSize = ref(1000)
	const total = ref(0)
	const demandProductTableList = ref([])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		demandProductTableList.value = []
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value,
			salesmanDemandId: id.value
		}
		if (pageNum.value > 1) {
			isLoading.value = true
			uni.showLoading({
				title: '数据加载中...'
			})
		}
		salesmanDemandProduct(params).then(res => {
			demandProductTableList.value = res.rows.filter(item => item.status !== null) || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
	  }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
</script>

<style scoped lang="scss">
.detail-text-integral {
	margin-left: 20rpx;
}
.detail-text-banners {
	margin-left: -10rpx;
}
.detail-text-banner {
	margin: 0 10rpx 10rpx;
}
.detail-text-video {
	width: 100%;
	height: 250rpx;
}
</style>
